package com.sgr.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.sgr.dao.EmpresaFiliadaDao;
import com.sgr.model.EmpresaFiliada;

	public class EmpresaFiliadaDaoImpl implements EmpresaFiliadaDao {
		
		@Autowired
		private SessionFactory sessionFactory;

		public void setSessionFactory(SessionFactory sessionFactory) {
			this.sessionFactory = sessionFactory;
		}
		
		public Session getCurrentSession(){
			return this.sessionFactory.getCurrentSession();
		}

		@Override
		@Transactional
		public void save(EmpresaFiliada emprfil) {
			getCurrentSession().save(emprfil);
		}

		@Override
		@Transactional
		public void update(EmpresaFiliada emprfil) {
			getCurrentSession().update(emprfil);
		}

		@Override
		@Transactional
		public void delete(EmpresaFiliada emprfil) {
			getCurrentSession().delete(emprfil);
		}

		@Override
		@Transactional
		public List findByName(String name) {
			return null;
		}

		@Override
		@Transactional
		public Long count() {
			Long rows = (Long) getCurrentSession().createCriteria(EmpresaFiliada.class).setProjection(Projections.rowCount()).uniqueResult();
			return rows;
		}

		@Override
		@Transactional
		public List findAll() {
			List empresafiliada = getCurrentSession().createQuery("from EmpresaFiliada").list();
			return empresafiliada;
		}

		@Override
		@Transactional
		public EmpresaFiliada findByPk(int id) {
			EmpresaFiliada empresafiliada =  (EmpresaFiliada) getCurrentSession().createCriteria(EmpresaFiliada.class).add(Restrictions.eq("id", id)).uniqueResult();
			return empresafiliada;
		}
		
}
